
#include "Deck.h"
#include <ctime>
#include <cassert>
#include <algorithm>
#include <iostream>

using namespace RackNetPoker;

CardDeck::CardDeck()
: m_nextCardIndex(0)
{
	_CreateDeck();
	Shuffle();
}

void CardDeck::Shuffle()
{
	m_nextCardIndex = 0;

	srand( static_cast<unsigned int>(time(NULL)) );

	const size_t swapNumbers = 100 + rand() % 100;

	for(size_t i = 0; i < swapNumbers; ++i)
	{
		const size_t fromIndex = rand() % 52;
		const size_t toIndex   = rand() % 52;

		std::swap<Card>(m_cards[fromIndex], m_cards[toIndex]);
	}

	//ToString();
}

const Card& CardDeck::PopCard() const
{
	assert(m_nextCardIndex < 52 && m_nextCardIndex >= 0);
	return m_cards[m_nextCardIndex++];
}

void CardDeck::_CreateDeck()
{
	for(size_t i = 0; i < 13; ++i)
	{
		Card c;
		c.m_cardValue = static_cast<Card::CardValue>(i);

		c.m_cardSuite = Card::CS_CLUBS;
		m_cards[0  + i] = c;

		c.m_cardSuite = Card::CS_DIAMONDS;
		m_cards[13 + i] = c;

		c.m_cardSuite = Card::CS_HEARTS;
		m_cards[26 + i] = c;

		c.m_cardSuite = Card::CS_SPADES;
		m_cards[39 + i] = c;
	}

	//ToString();
}

void CardDeck::ToString() const
{
	for(size_t i = 0; i < 52; ++i)
	{
		std::cout << m_cards[i].m_cardSuite << " " << m_cards[i].m_cardValue << std::endl;
	}
}